window.onload = function () {
    document.getElementById("message").onclick = function () {
        var a = document.querySelectorAll('dl>dd')
        for(i=0;i<4;i++){
            a[i].classList.toggle("hide")
        }
    }
    document.getElementById("superSail").onclick = function () {
        var a = document.querySelectorAll('dl>dd')
        for(i=4;i<6;i++){
            a[i].classList.toggle("hide")
        }
    }
    document.getElementById("order").onclick = function () {
        var a = document.querySelectorAll('dl>dd')
        for(i=6;i<a.length;i++){
            a[i].classList.toggle("hide")
        }
    }
}
function reset(){
    document.getElementById("email").value=``
    document.getElementById("password").value=``
    document.getElementById("newPassword").value=``
}
function changePassword(){
    var email = document.getElementById("email").value
    var password = document.getElementById("password").value
    var newPassword = document.getElementById("newPassword").value
    console.log(password);
    console.log(newPassword);
    axios.put('/home/safe/user/updatePassword',{
        params:{
            password:password,
            newPassword:newPassword
        }
    }).then(function(data){
        if(data.data.data.code==20000){
            document.getElementById("floatContent").innerHTML ="<div>"+"修改成功"+"</div>"+"<button onclick='closethis()' class='newbtn'>"+"确定"+"</button>";
            document.getElementById("floatContent").classList.remove("hide");
        }else{
            alert(1)
            document.getElementById("floatContent").innerHTML ="<div>"+ "很抱歉"+"</div>"+"<div>"+"修改失败，请联系工作人员"+"</div>"+"<button onclick='closethis()' class='newbtn'>"+"确定"+"</button>";
            document.getElementById("floatContent").classList.remove("hide");
        }
    }).catch(function (error){
        console.log(error)
        document.getElementById("floatContent").innerHTML ="<div>"+ "很抱歉"+"</div>"+"<div>"+"请检查网络情况"+"</div>"+"<button onclick='closethis()' class='newbtn'>"+"确定"+"</button>";
        document.getElementById("floatContent").classList.remove("hide");
    })
}
function close() {
    document.getElementById("floatContent").classList.add("hide");
} 
var  send = () => {
    document.getElementById("floatContent").innerHTML = `
    <div class="floatTop">请输入图片中的数字</div>
    <button id="close" onclick="click()"><i class="fa fa-times  fa-2x" aria-hidden="true"></i></button>
    <canvas id="canvas" onclick="dr()"></canvas>
    <input type="codetext" id="codetext" name="ps" class="inputFirst" placeholder="请输入验证码">
    <button id="yes" onclick="sublim() ; close()">确定</button>
    </div>`
    document.getElementById("floatContent").classList.remove("hide");
    document.getElementById("close").onclick = function () {
        close()        
    }
    draw(show_num);  
}
var show_num = [];
function dr(){
draw(show_num);   
}
function sublim(){
    var val=document.getElementById("codetext").value;  
    var num = show_num.join("");//把数组元素转换为字符串
    if(val==''){
        alert('请输入验证码！');
    }else if(val == num){
        alert('提交成功！');
        sendma();
    }else{
        alert('验证码错误！请重新输入！');
        document.getElementById("codetext").value='';
        draw(show_num);
    }
}
function draw(show_num) {
    var canvas_width=document.getElementById('canvas').clientWidth;
    var canvas_height=document.getElementById('canvas').clientHeight;
    var canvas = document.getElementById("canvas");//获取到canvas的对象，演员
    var context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
    canvas.width = canvas_width;
    canvas.height = canvas_height;
    var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0,q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m";
    var aCode = sCode.split(",");
    var aLength = aCode.length;//获取到数组的长度
        
    for (var i = 0; i <= 3; i++) {
        var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
        var deg = (Math.random() * 30 * Math.PI / 180); //产生0~30之间的随机弧度
        var txt = aCode[j];//得到随机的一个内容
        show_num[i] = txt;
        var x = 10 + i * 20;//文字在canvas上的x坐标 
        var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
        context.font = "bold 23px 微软雅黑";    //文字样式

        context.translate(x, y);
        context.rotate(deg);

        context.fillStyle = randomColor();
        context.fillText(txt, 0, 0);     //加入文本

        context.rotate(-deg);            //角度变换
        context.translate(-x, -y);       //位置变换
    }
    for (var i = 0; i <= 5; i++) { //验证码上显示线条 ，5条线
        context.strokeStyle = randomColor();
        context.beginPath();         //开始一条路径
        context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
        context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);   //创建从哪到哪的一条线
        context.stroke();          //绘制一条路径
    }
    for (var i = 0; i <= 30; i++) { //验证码上显示小点 ，30个点
        context.strokeStyle = randomColor();
        context.beginPath();         //开始一条路径
        var x = Math.random() * canvas_width;
        var y = Math.random() * canvas_height;
        context.moveTo(x, y);
        context.lineTo(x + 1, y + 1);//同样是线，但是因为长度为1，就变成了点
        context.stroke();           //绘制一条路径
    }
}
function randomColor() {//得到随机的颜色值
    var r = Math.floor(Math.random() * 256);
    var g = Math.floor(Math.random() * 256);
    var b = Math.floor(Math.random() * 256);
    return "rgb(" + r + "," + g + "," + b + ")";
}
function sendma(){
    var email = document.getElementById("email").value;
    var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if (!myreg.test(email)) {
        // document.getElementById("floatContent").classList.remove("hide")
        console.log( document.getElementById("floatContent"));
        console.log(document.getElementById("floatContent").classList);
        document.getElementById("floatContent").innerHTML = `<div>提示\n请输入有效的E_mail!</div><button onclick='closethis();return false' class='newbtn'>确定</button>`
        document.getElementById("floatContent").classList.remove("hide")
        // alert(document.getElementById("floatContent").classList)
        // alert(document.getElementById("floatContent").classList)
    } else {
        // alert(2)
        axios.post('/home/register/email/sendAuthCodeEmail',{
            params:{
                email: email,
            }
        }).then(function (data) {
            var totalTime = 60;
            if (data.data.data.code == 20000) {
                document.getElementById("send").style.backgroundColor='lightgray'
                document.getElementById("send").style.color='white'
                // document.getElementById("noclick").style.display='block'
                let clock = window.setInterval(() =>{
                    totalTime--;
                    document.getElementById("send").innerHTML = totalTime+'s后重新发送'
                    if(totalTime == 0){
                        document.getElementById("send").style.backgroundColor='#F96C0C'
                        document.getElementById("send").style.color='white'
                        // document.getElementById("noclick").style.display='none'
                        window.clearInterval(clock)
                        document.getElementById("send").innerHTML = '重新发送'
                        totalTime = 60;
                    }
                },1000)
                document.getElementById("floatContent").innerHTML = "<div>" + "验证码已发送" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
                document.getElementById("floatContent").classList.remove("hide");
            }else{
                document.getElementById("floatContent").innerHTML = "<div>" + "验证码发送失败" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
                document.getElementById("floatContent").classList.remove("hide");
            }
        }).catch(function (error) {
            console.log(error)
            document.getElementById("floatContent").innerHTML = "<div>" + "请检查网络状况" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
            document.getElementById("floatContent").classList.remove("hide");
        })
    };
}
function closethis(){
    document.getElementById("floatContent").classList.add("hide");
}